package one.stand.service.impl;

import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.mapper.GoodsMapperExt;
import one.stand.mapper.GoodsSelectMapperExt;
import one.stand.model.Goods;
import one.stand.service.GoodsSelectServcie;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@Slf4j
public class GoodsSelectServcieImpl implements GoodsSelectServcie {
    @Autowired
    private GoodsMapperExt goodsMapperExt;
    @Autowired
    private GoodsSelectMapperExt goodsSelectMapperExt;

    @Override
    @Transactional
    public void copyGoods(Integer companyId, Integer selectCompanyId) {
        //save type
        goodsSelectMapperExt.insertTypes(companyId, selectCompanyId);
        goodsSelectMapperExt.updateTypes(companyId, selectCompanyId);

        //save goods
        goodsSelectMapperExt.insertGoods(companyId, selectCompanyId, null);
        goodsSelectMapperExt.updateGoodsAgree(companyId, selectCompanyId, null);
        goodsSelectMapperExt.insertSpecs(companyId, selectCompanyId, null);
        goodsSelectMapperExt.updateSpecs(companyId, selectCompanyId, null);

        //update goods
        goodsSelectMapperExt.updateGoodsAgree(companyId, selectCompanyId, null);

        //photo
        goodsSelectMapperExt.insertPhoto(companyId, selectCompanyId);

        //banner
        goodsSelectMapperExt.insertBanner(companyId, selectCompanyId);
        goodsSelectMapperExt.updateBanner(companyId, selectCompanyId);
    }

    @Override
    @Transactional
    public void copyGoodsOne(Integer companyId, Integer goodsId) {
        log.info("goods selected:{} {}", companyId, goodsId);
        final int isGoodsSelected = isSelected(companyId, goodsId);
        AssertUtil.isFalse(isGoodsSelected == 0, "无法代理");
        AssertUtil.isFalse(isGoodsSelected == 1, "已代理");

        Goods goods = goodsMapperExt.selectByPrimaryKey(goodsId);

        //save type
        goodsSelectMapperExt.insertTypesGoods(companyId, goods.getCompanyId(), goodsId);
        goodsSelectMapperExt.updateTypes(companyId, goods.getCompanyId());

        //save goods
        goodsSelectMapperExt.insertGoods(companyId, goods.getCompanyId(), goodsId);
        goodsSelectMapperExt.updateGoodsAgree(companyId, goods.getCompanyId(), goodsId);
        goodsSelectMapperExt.insertSpecs(companyId, goods.getCompanyId(), goodsId);
        goodsSelectMapperExt.updateSpecs(companyId, goods.getCompanyId(), goodsId);

        //update goods
        goodsSelectMapperExt.updateGoodsAgree(companyId, goods.getCompanyId(), goodsId);

        //photo
        goodsSelectMapperExt.insertPhoto(companyId, goods.getCompanyId());
    }

    @Override
    @Transactional
    public void removeGoodsOne(Integer companyId, Integer goodsId) {
        AssertUtil.nonNull(companyId, ResultEnum.FAIL);
        AssertUtil.nonNull(goodsId, ResultEnum.FAIL);
        Goods goods = goodsMapperExt.selectByPrimaryKey(goodsId);
        AssertUtil.isTrue(Objects.nonNull(goods) && goods.getCompanyId().equals(companyId), ResultEnum.FAIL);

        Goods goodsUpdate = new Goods();
        goodsUpdate.setGoodsId(goodsId);
        goodsUpdate.setIsDel(1);
        goodsMapperExt.updateByPrimaryKeySelective(goodsUpdate);
    }

    @Override
    @Transactional
    public void removeGoodsSelect(Integer companyId, Integer selectCompanyId) {
        //remove goods
        goodsSelectMapperExt.deleteByCopy(companyId, selectCompanyId);
        //remove type
        goodsSelectMapperExt.deleteTypes(companyId, selectCompanyId);
        //remove spec
        goodsSelectMapperExt.deleteSpecCopy(companyId);
        //photo
        goodsSelectMapperExt.updatePhoto(companyId, selectCompanyId, null);
        //banner
        goodsSelectMapperExt.deleteAllBanner(companyId, selectCompanyId);
    }

    @Override
    @Transactional
    public void updateType(Integer selectCompanyId) {
        goodsSelectMapperExt.insertTypes(null, selectCompanyId);
        goodsSelectMapperExt.updateTypes(null, selectCompanyId);
    }

    @Override
    @Transactional
    public void updateGoods(Integer selectCompanyId, Integer goodsId) {
        if (Objects.isNull(selectCompanyId) && Objects.nonNull(goodsId) && goodsId > 0) {
            Goods goods = goodsMapperExt.selectByPrimaryKey(goodsId);
            selectCompanyId = goods.getCompanyId();
        }
        goodsSelectMapperExt.updateGoodsRightReduction(null, selectCompanyId, goodsId);
        goodsSelectMapperExt.insertGoods(null, selectCompanyId, goodsId);

        goodsSelectMapperExt.insertSpecs(null, selectCompanyId, goodsId);
        goodsSelectMapperExt.updateSpecs(null, selectCompanyId, goodsId);

        //update goods
        goodsSelectMapperExt.updateGoods(null, selectCompanyId, goodsId);

        //photo
        if (Objects.nonNull(goodsId)) {
            goodsSelectMapperExt.updatePhoto(null, selectCompanyId, goodsId);
        }
        goodsSelectMapperExt.insertPhoto(null, selectCompanyId);
    }

    @Override
    @Async("asyncServiceExecutor")
    @Transactional
    public void updateBanner(Integer selectCompanyId, Integer deleteId) {
        if (Objects.nonNull(selectCompanyId)) {
            goodsSelectMapperExt.insertBanner(null, selectCompanyId);
            goodsSelectMapperExt.updateBanner(null, selectCompanyId);
        } else if (Objects.nonNull(deleteId)) {
            goodsSelectMapperExt.deleteBanner(deleteId);
        }
    }

    @Override
    public int isSelected(Integer companyId, Integer goodsId) {
        Goods goods = goodsMapperExt.selectByPrimaryKey(goodsId);
        if (Objects.nonNull(goods.getSelectGoodsId())) return 0;
        if (Objects.equals(companyId, goods.getCompanyId())) return 0;
        Integer id = goodsMapperExt.selectSelectGoods(companyId, goodsId);
        return Objects.nonNull(id) ? 1 : 2;
    }

    @Override
    public Integer getBySelectGoodsId(Integer companyId, Integer selectGoodsId) {
        return goodsMapperExt.selectSelectGoods(companyId, selectGoodsId);
    }
}
